<template>
  <n-el tag="dTopTabsiv" class="layout-main">
    <PictrueList
      ref="pictureListRef"
      :left-top-operate="leftTopOperate"
      :data-params="{
        exclude_url: userInfo.pic_url,
      }"
    ></PictrueList>
  </n-el>
</template>
<script lang="ts" setup>
import { Ref, ref, watch } from "vue";
import PictrueList from "@/app/admin/components/PictrueList.vue";
import { Bookmark, RemoveCircle } from "@vicons/ionicons5";
import { moveInExcellent, moveInMainPic, moveOut, moveOutExcellent, moveOutMainPic, moveOutOpenScreenPic, moveInOpenScreenPic } from "../../api/pictrue";
import appStore from "@/packages/pinia/app.ts";
const { userInfo } = appStore();

const pictureListRef: any = ref(null);

const handlerOpenScreenPic = (item: any) => {
  if (item.is_banner === "1") {
    moveOutOpenScreenPic({ pid: item.pid }).then((res: any) => {
      if (res.status === "success") {
        window.$message.success(res.msg || "删除成功");
        item.is_banner = "0";
      } else {
        window.$message.error(res.msg || "删除失败");
      }
    });
  } else {
    moveInOpenScreenPic({ pid: item.pid }).then((res: any) => {
      if (res.status === "success") {
        window.$message.success(res.msg ||"添加成功");
        item.is_banner = "1";
      } else {
        window.$message.error(res.msg || "添加失败");
      }
    });
  }
};

const handlerMainPic = (item: any) => {
  if (item.is_main_pic === "1") {
    moveOutMainPic({ pid: item.pid }).then((res: any) => {
      if (res.status === "success") {
        window.$message.success(res.msg || "删除主图成功");
        item.is_main_pic = "0";
      } else {
        window.$message.error(res.msg || "删除主图失败");
      }
    });
  } else {
    moveInMainPic({ pid: item.pid }).then((res: any) => {
      if (res.status === "success") {
        window.$message.success(res.msg ||"添加主图成功");
        item.is_main_pic = "1";
      } else {
        window.$message.error(res.msg || "添加主图失败");
      }
    });
  }
};

const handlerExcellent = (item: any) => {
  if (item.is_excellent === "1") {
    moveOutExcellent({ pid: item.pid }).then((res: any) => {
      if (res.status === "success") {
        window.$message.success(res.msg || "移出精选成功");
        item.is_excellent = "0";
      } else {
        window.$message.error(res.msg || "移出精选失败");
      }
    });
  } else {
    moveInExcellent({ pid: item.pid }).then((res: any) => {
      if (res.status === "success") {
        window.$message.success(res.msg ||"移入精选成功");
        item.is_excellent = "1";
      } else {
        window.$message.error(res.msg || "移入精选失败");
      }
    });
  }
};

// 移出目录
const handlerMoveOut = (item: any) => {
  moveOut({ pid: item.pid }).then((res: any) => {
    if (res.status === "success") {
      window.$message.success("触发成功，不删除数据，只做不显示操作");
      item.is_show = "0";
      item.is_main_pic = "0";
      item.is_excellent = "0";
    } else {
      window.$message.error(res.msg || "移除失败");
    }
  }).catch((err: any) => {
    window.$message.error(err || "移除失败");
  });
};

const leftTopOperate = [
  { id: 0, handler: handlerOpenScreenPic, icon: Bookmark, tip: "设置为开屏图", isActive: (picture: any) => picture.is_banner === "1" },
  { id: 1, handler: handlerMainPic, icon: Bookmark, tip: "添加为主图", isActive: (picture: any) => picture.is_main_pic === "1" },
  { id: 2, handler: handlerExcellent, icon: Bookmark, tip: "移入精选", isActive: (picture: any) => picture.is_excellent === "1" },
  { id: 3, handler: handlerMoveOut, icon: RemoveCircle, tip: "移除-不显示" },
];
</script>

<style lang="less" scoped>
.layout-main {
  height: 100%;
  display: flex;
  flex-direction: column;
}
</style>
